package com.tmp;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.zip.GZIPInputStream;
import java.util.zip.InflaterInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

public class CompressionHandler {
    public static void main(String[] args) throws Exception {
        String url = "http://222.247.54.188:8090/fmd/fd/phis/queryList?start=0&limit=10"; // 测试地址
        
        // 创建 HTTP 请求,request头部添加token

        String jsonInputString = "61055049ea8f3948c6f2602a0d739901086f18f4c17781747a63aad8a8fd124f370f41e5a9eb4d30c6e2c7d276af822d4219b0cc0f9590c312ce05d5f7fcd8ed272c217d1cbbeee9e91a311a5fd19de3937e26e62b741d230dac75b06ac135f163815816f4c49c465f534029fbd94e306140ea0cd1095a6bcd1547082ecd258ea7f73257c8acde13daa36791732c44d6ffa08370be2b1656f06f5835169126ce8f7a05b0391df6685ede005f5bc41fa499624270696516917b0694478d133f56958c6c3d1ea10cb4ab864cbff4313f5b0652088fa387335dc66e999a0601940d506f243d4bfb0b1cc14366004bb7ca7cbe5416ea026e2618dfef80207ec657c4f1f2a2f8e0fdadb3e4b96292e51e0e73";

        ;

        //使用HttpRequest.newBuilder() 添加多个请求头
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .header("Accept-Encoding", "gzip, deflate") // 声明支持的压缩类型
                .headers("Content-Type", "application/json")
                .headers("Accept-Encoding", "gzip, deflate")
                .headers("Sign", "OXyrz0ZfCCCaMpC3DtpR4UgihVEjo0KU3bwV74/sxZCXkZDrq/HEyA9fFQA28avsYuFl/F4FUhGxMoyaZXuDmuVNqxfDXdQzQTMncJXc5CFbo+o9y7tqFh6Rq8CNCUQLunHn5+ZFqbiiwgRSSSGzAShJH01lVf/vIB9aQvzMlYg=")
                .headers("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36")
                .headers("Token", "175e6e55-911f-4ba2-b03f-6cfc40b68813")
                .headers("Content-Length", "544")
                .POST(HttpRequest.BodyPublishers.ofString(jsonInputString))
                .build();




        // 发送请求并获取响应
        HttpClient client = HttpClient.newHttpClient();
        HttpResponse<InputStream> response = client.send(request, 
            HttpResponse.BodyHandlers.ofInputStream());

        // 获取压缩类型
        String encoding = response.headers().firstValue("Content-Encoding").orElse("none");
        System.out.println("检测到压缩类型: " + encoding);
//        String encoding ="gzip";
//        String encoding ="deflate";

        // 自动解压处理
        try (InputStream is = response.body()) {
            String content = switch (encoding.toLowerCase()) {
                case "gzip" -> readStream(new GZIPInputStream(is));
                case "deflate" -> readStream(new InflaterInputStream(is));
                default -> readStream(is); // 无压缩直接读取
            };
            System.out.println("解压后的内容:\n" + content);
        }
    }

    // 通用流读取方法（UTF-8 编码）
    private static String readStream(InputStream is) throws IOException {
        return new String(is.readAllBytes(), "UTF-8");
    }
}
